<template>
    <div v-if="isOpen" class="modal-overlay">
        <div class="modal-container">
            <div class="modal-header">
                <div class="modal-title">{{ modalTitle }}</div>
                <button class="modal-close" @click="onClose">×</button>
            </div>
            <div class="modal-content">
                <!-- 文本输入类型 -->
                <div v-if="inputType === 'text'" class="input-container">
                    <input
                        type="text"
                        class="form-input"
                        v-model="inputValue"
                    />
                    <button class="clear-input" @click="clearInput">×</button>
                </div>

                <!-- 下拉选择类型 -->
                <select 
                    v-else-if="inputType === 'select'"
                    class="form-select"
                    v-model="inputValue"
                >
                    <option 
                        v-for="option in options" 
                        :key="option.value"
                        :value="option.value"
                    >
                        {{ option.label }}
                    </option>
                </select>

                <!-- 日期选择类型 -->
                <input
                    v-else-if="inputType === 'date'"
                    type="date"
                    class="form-input"
                    v-model="inputValue"
                />

                <!-- 数字输入类型 -->
                <div v-else-if="inputType === 'number'" class="input-container">
                    <input
                        type="number"
                        class="form-input"
                        v-model="inputValue"
                    />
                    <span class="unit">{{ unit }}</span>
                </div>
            </div>
            <div class="modal-footer">
                <button class="submit-button" @click="handleSubmit">提交</button>
                <button class="skip-button" @click="onSkip">跳过</button>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, watch } from 'vue';

const props = defineProps({
    modalTitle: {
        type: String,
        required: true
    },
    initialValue: {
        type: [String, Number],
        default: ''
    },
    isOpen: {
        type: Boolean,
        default: false
    },
    inputType: {
        type: String,
        default: 'text',
        validator: value => ['text', 'select', 'date', 'number'].includes(value)
    },
    options: {
        type: Array,
        default: () => []
    },
    unit: {
        type: String,
        default: ''
    }
});

const emits = defineEmits(['submit', 'skip', 'close']);

const inputValue = ref(props.initialValue);

// 当初始值变化时更新输入值
watch(() => props.initialValue, (newVal) => {
    inputValue.value = newVal;
});

const clearInput = () => {
    inputValue.value = '';
};

const handleSubmit = () => {
    emits('submit', inputValue.value);
    emits('close');
};

const onClose = () => {
    emits('close');
};

const onSkip = () => {
    emits('skip');
    emits('close');
};
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-container {
  width: 90%;
  max-width: 350px;
  background-color: white;
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.modal-header {
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #f0f0f0;
}

.modal-title {
  font-size: 18px;
  font-weight: bold;
}

.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  color: #999;
  cursor: pointer;
}

.modal-content {
  padding: 20px;
}

.nickname-input-container {
  position: relative;
  margin-bottom: 20px;
}

.nickname-input {
  width: 100%;
  padding: 12px 40px 12px 15px;
  border-radius: 8px;
  border: none;
  background-color: #f5f5f5;
  font-size: 16px;
}

.clear-input {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: #999;
  font-size: 18px;
  cursor: pointer;
}

.modal-footer {
  display: flex;
  flex-direction: column;
  padding: 0 20px 20px 20px;
}

.submit-button {
  background-color: #6366f1;
  color: white;
  border: none;
  border-radius: 25px;
  padding: 12px;
  font-size: 16px;
  margin-bottom: 10px;
  cursor: pointer;
}

.skip-button {
  background: none;
  border: none;
  color: #6366f1;
  font-size: 14px;
  cursor: pointer;
}
.modal-overlay {
    /* 保持原有样式 */
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.modal-container {
    background: white;
    border-radius: 8px;
    width: 90%;
    max-width: 400px;
    padding: 20px;
}

.input-container {
    position: relative;
    display: flex;
    align-items: center;
}

.form-input {
    width: 100%;
    padding: 12px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 16px;
}

.form-select {
    width: 100%;
    padding: 12px;
    border: 1px solid #ddd;
    border-radius: 4px;
    background: white;
    appearance: none;
}

.clear-input {
    position: absolute;
    right: 10px;
    background: none;
    border: none;
    font-size: 20px;
    color: #999;
    cursor: pointer;
}

.unit {
    position: absolute;
    right: 10px;
    color: #666;
}

.modal-footer {
    margin-top: 20px;
    display: flex;
    gap: 10px;
    justify-content: flex-end;
}

.submit-button, .skip-button {
    padding: 8px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

.submit-button {
    background: #007bff;
    color: white;
}

.skip-button {
    background: #f0f0f0;
    color: #666;
}
</style>    